package com.copd.controller;

import com.copd.common.Result;
import com.copd.entity.Role;
import com.copd.entity.Permission;
import com.copd.service.RoleService;
import com.copd.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "医生权限管理")
@RestController
@RequestMapping("/api/doctor-permissions")
public class DoctorPermissionController {
    
    private static final Logger logger = LoggerFactory.getLogger(DoctorPermissionController.class);
    
    @Autowired
    private RoleService roleService;
    
    @Autowired
    private PermissionService permissionService;
    
    @ApiOperation(value = "获取医生角色", notes = "根据医生ID获取其角色")
    @GetMapping("/{doctorId}/roles")
    public Result<List<Role>> getDoctorRoles(
            @ApiParam(value = "医生ID", required = true) @PathVariable Integer doctorId) {
        try {
            List<Role> roles = roleService.getRolesByUserId(doctorId);
            return Result.success("获取医生角色成功", roles);
        } catch (Exception e) {
            logger.error("获取医生角色失败", e);
            return Result.error("500", "获取医生角色失败");
        }
    }
    
    @ApiOperation(value = "获取医生权限", notes = "根据医生ID获取其权限")
    @GetMapping("/{doctorId}/permissions")
    public Result<List<Permission>> getDoctorPermissions(
            @ApiParam(value = "医生ID", required = true) @PathVariable Integer doctorId) {
        try {
            List<Permission> permissions = permissionService.getPermissionsByUserId(doctorId);
            return Result.success("获取医生权限成功", permissions);
        } catch (Exception e) {
            logger.error("获取医生权限失败", e);
            return Result.error("500", "获取医生权限失败");
        }
    }
    
    @ApiOperation(value = "设置医生角色", notes = "为医生分配角色")
    @PostMapping("/{doctorId}/roles/{roleId}")
    public Result<Boolean> assignRoleToDoctor(
            @ApiParam(value = "医生ID", required = true) @PathVariable Integer doctorId,
            @ApiParam(value = "角色ID", required = true) @PathVariable Integer roleId) {
        try {
            // 先移除医生的所有角色
            List<Role> userRoles = roleService.getRolesByUserId(doctorId);
            for (Role role : userRoles) {
                roleService.removeRoleFromUser(doctorId, role.getRoleId());
            }
            
            // 分配新角色
            boolean assigned = roleService.assignRoleToUser(doctorId, roleId);
            if (assigned) {
                return Result.success("分配医生角色成功", true);
            }
            return Result.error("500", "分配医生角色失败");
        } catch (Exception e) {
            logger.error("分配医生角色失败", e);
            return Result.error("500", "分配医生角色失败");
        }
    }
    
    @ApiOperation(value = "获取所有可用角色", notes = "获取系统中所有可用角色")
    @GetMapping("/available-roles")
    public Result<List<Role>> getAvailableRoles() {
        try {
            List<Role> roles = roleService.getAllRoles();
            return Result.success("获取可用角色成功", roles);
        } catch (Exception e) {
            logger.error("获取可用角色失败", e);
            return Result.error("500", "获取可用角色失败");
        }
    }
    
    @ApiOperation(value = "获取所有可用权限", notes = "获取系统中所有可用权限")
    @GetMapping("/available-permissions")
    public Result<List<Permission>> getAvailablePermissions() {
        try {
            List<Permission> permissions = permissionService.getAllPermissions();
            return Result.success("获取可用权限成功", permissions);
        } catch (Exception e) {
            logger.error("获取可用权限失败", e);
            return Result.error("500", "获取可用权限失败");
        }
    }
} 